package com.joker.web.controller.orange;

import java.util.List;
import java.util.Arrays;

import com.joker.common.core.domain.entity.SysRole;
import com.joker.common.helper.LoginHelper;
import com.joker.system.domain.SysUserRole;
import com.joker.system.mapper.SysUserRoleMapper;
import com.joker.system.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.joker.common.annotation.RepeatSubmit;
import com.joker.common.annotation.Log;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.validate.AddGroup;
import com.joker.common.core.validate.EditGroup;
import com.joker.common.enums.BusinessType;
import com.joker.common.utils.poi.ExcelUtil;
import com.joker.orange.domain.vo.AgentUserVo;
import com.joker.orange.domain.bo.AgentUserBo;
import com.joker.orange.service.IAgentUserService;
import com.joker.common.core.page.TableDataInfo;

/**
 * 代理信息
 *
 * @author Joker
 * @date 2024-08-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agent/user")
public class AgentUserController extends BaseController {

    private final IAgentUserService iAgentUserService;
    private final ISysRoleService iSysRoleService;
    private final SysUserRoleMapper userRoleMapper;

    /**
     * 查询代理信息列表
     */
    @SaCheckPermission("agent:user:list")
    @GetMapping("/list")
    public TableDataInfo<AgentUserVo> list(AgentUserBo bo, PageQuery pageQuery) {
        return iAgentUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出代理信息列表
     */
    @SaCheckPermission("agent:user:export")
    @Log(title = "代理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgentUserBo bo, HttpServletResponse response) {
        List<AgentUserVo> list = iAgentUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理信息", AgentUserVo.class, response);
    }

    /**
     * 获取代理信息详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("agent:user:query")
    @GetMapping("/{userId}")
    public R<AgentUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(iAgentUserService.queryById(userId));
    }

    /**
     * 新增代理信息
     */
    @SaCheckPermission("agent:user:add")
    @Log(title = "代理信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgentUserBo bo) {
        Long roleId = 0L;
        if (bo.getAgentLevel().equals("1")){
            SysRole role = iSysRoleService.selectRoleByRoleKey("firstAgent");
            roleId = role.getRoleId();
        }
        if (bo.getAgentLevel().equals("2")){
            SysRole role = iSysRoleService.selectRoleByRoleKey("twoAgent");
            roleId = role.getRoleId();
        }

        Long userId = iAgentUserService.insertByBo(bo);
        SysUserRole ur = new SysUserRole();
        ur.setUserId(userId);
        ur.setRoleId(roleId);
        userRoleMapper.insert(ur);
        return R.ok();
    }

    /**
     * 修改代理信息
     */
    @SaCheckPermission("agent:user:edit")
    @Log(title = "代理信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgentUserBo bo) {
        return toAjax(iAgentUserService.updateByBo(bo));
    }

    /**
     * 删除代理信息
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("agent:user:remove")
    @Log(title = "代理信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(iAgentUserService.deleteWithValidByIds(Arrays.asList(userIds), true));
    }

    @GetMapping("/getUserInfo")
    public R<AgentUserVo> getUserInfo() {
        return R.ok(iAgentUserService.queryById(LoginHelper.getUserId()));
    }

    @GetMapping("/getUserList")
    public R<List<AgentUserVo>> getUserList() {
        return R.ok(iAgentUserService.queryList(new AgentUserBo()));
    }
}
